import Vue from 'vue'
Vue.mixin({
  props: {
    operType: {
      type: String,
      default: ''
    }
  },
  data () {
    return {
      prePageRouteName: ''
    }
  },
  created () {
    this.$options.init && this.$options.init.call(this)
    // 监听回调广播
    if (
      this.$route &&
      this.$route.name &&
      this.$route.name === this.$options.name
    ) {
      this.$root.$on(this.$route.name + '_callback', arg => {
        const methods = this.$options.methods
        if (methods) {
          if (methods.pageCallBack) {
            methods.pageCallBack.call(this, arg)
          } else if (methods.refreshTable) {
            methods.refreshTable.call(this, arg)
          }
        }
      })
    }
    // if (this.$route && this.$route.name === this.$options.name && this.$root.prePageRouteName) {
    //   this.prePageRouteName = this.$root.prePageRouteName
    //   console.log('AAA2', this)
    // }
  },
  computed: {
    look () {
      return this.operType === 'look'
    },
    edit () {
      return this.operType === 'edit'
    },
    create () {
      return this.operType === 'create'
    }
  },
  methods: {
    resetFields (ref) {
      const refForm = this.$refs[ref]
      if (refForm && refForm.resetFields) {
        refForm.resetFields()
      }
    },
    refreshTable (ref) {
      const refTable = this.$refs[ref]
      if (refTable && refTable.resetFields) {
        refTable.refresh()
      }
    },
    closeTab (arg, back) {
      if (arg) {
        this.prePageCallBack(arg)
      }
      const latestView = this.$store.state.tagsView.visitedViews[this.$store.state.tagsView.visitedViews.length - 1]
      var backUrl = ''
      for (var i = this.$store.state.tagsView.visitedViews.length - 1; i >= 0; i--) {
        if (latestView.path !== this.$store.state.tagsView.visitedViews[i].path) {
          if (latestView.path.indexOf(this.$store.state.tagsView.visitedViews[i].path) !== -1) {
            backUrl = this.$store.state.tagsView.visitedViews[i].path
            break
          }
        }
      }
      this.$store.dispatch('delVisitedViews', this.$route)
      if (backUrl) {
        this.$router.push({ path: backUrl, query: { backLoad: back } })
      } else {
        this.$router.push('/index')
      }
    },
    prePageCallBack (arg) {
      if (this.prePageRouteName) {
        this.$root.$emit(this.prePageRouteName + '_callback', arg)
      }
    },
    fullScreenLoading (loading) {
      const fullscreenloading = this.$loading({
        lock: true,
        fullscreen: true,
        text: '给我一点点时间~~'
      })
      setTimeout(() => {
        fullscreenloading.close(!loading)
      }, 1000)
    },
    queryed (query) {
      let queryed = 0
      if (query) {
        for (var i in query) {
          if (query[i]) {
            queryed += 1
          } else {
            queryed += 0
          }
        }
      }
      if (queryed !== 0) {
        return true
      } else {
        return false
      }
    },
    confirmBox (confirmParams) {
      // 默认参数
      //    okText, cancelText, title, message, type, callbackOk, callbackCancel
      //    确定文字，取消文字，标题，内容，图标类型，确认回调，取消回调
      this.$confirm(confirmParams.message, confirmParams.title, {
        customClass: 'confirm-box',
        confirmButtonText: confirmParams.okText || '确认',
        cancelButtonText: confirmParams.cancelText || '取消',
        lockScroll: true,
        closeOnClickModal: false,
        center: true,
        showClose: false,
        type: confirmParams.type || '',
        callback: confirmParams.callback
      })
      //    .then(() => {
      //            callback.callbackOk && callback.callbackOk()
      //     }).catch(() => {
      //            callback.callbackCancel && callback.callbackCancel()
      //          })
    },
    messageBox (messageParams) {
      this.$message({
        message: messageParams.message,
        center: true,
        showClose: false,
        duration: 1500,
        customClass: 'message-box',
        type: undefined
      })
      setTimeout(() => {
        this.$message.closeAll()
      }, 1500)
    }
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {
      if (from.name && vm.$route.name === vm.$options.name) {
        // vm.$root.prePageRouteName = from.name
        vm.prePageRouteName = from.name
      }
    })
  },
  activated () {
    this.$options.init && this.$options.init.call(this)
    // if (this.$route.fullPath !== this.$fullPath) { this.$options.init && this.$options.init.call(this) }
    // this.$fullPath = this.$route.fullPath
  }

})
